<template>
  <!-- 风险辨识清单 -->
  <div class="riskpoint">
    <basic-container>
      <avue-crud
        :option="option"
        :table-loading="loading"
        :data="data"
        :page.sync="page"
        :before-open="beforeOpen"
        v-model="form"
        ref="crud"
        @search-change="searchChange"
        @search-reset="searchReset"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
      >
        <!-- 左上方菜单 -->
        <template slot="menuLeft">
          <el-button
            type="warning"
            size="small"
            plain
            icon="el-icon-download"
            @click="handleExport"
          >
            导 出
          </el-button>
        </template>

        <!-- 主要事故表格自定义 -->
        <template slot="zysglx" slot-scope="scope">
          <div>{{ formatMainFault(scope.row) }}</div>
        </template>

        <!-- 风险等级表格自定义 -->
        <template slot="fxdj" slot-scope="scope">
          <div>
            <div
              :class="{
                fxdj_a: fxdjObj[scope.row['fxdj']] === 'A',
                fxdj_b: fxdjObj[scope.row['fxdj']] === 'B',
                fxdj_c: fxdjObj[scope.row['fxdj']] === 'C',
                fxdj_d: fxdjObj[scope.row['fxdj']] === 'D'
              }"
            >
              {{ fxdjObj[scope.row["fxdj"]] }}
            </div>
          </div>
        </template>

        <!-- 现场照片表格自定义 -->
        <template slot="xczp" slot-scope="scope">
          <div class="ope_row_div">
            <span
              v-if="
                scope.row['xczp'] === '' || scope.row['bladeFiles'].length === 0
              "
              >-</span
            >
            <el-button v-else type="text" @click="handleViewPhoto(scope.row)"
              >查看</el-button
            >
          </div>
        </template>

        <!-- 评估状态表格自定义 -->
        <template slot="isassess" slot-scope="scope">
          <div>
            <div class="not_assess" v-if="scope.row['isassess'] === '2'">
              待评估
            </div>
            <div class="is_assess" v-if="scope.row['isassess'] === '1'">
              已评估
            </div>
          </div>
        </template>

        <!-- 地图表单自定义 -->
        <template slot="mapForm">
          <div>
            <baidu-map class="map" :center="center" :zoom="zoom">
              <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>
              <bm-marker :position="mapPosition" :dragging="true"></bm-marker>
            </baidu-map>
          </div>
        </template>

        <!-- 警示标志表单自定义 -->
        <template slot="jsbzForm">
          <div class="jsbz_content">
            <el-tag
              v-for="(item, index) in jsbzFileList"
              :key="index"
              type="info"
              :closable="false"
              @close="handleRemoveJSBZ(index)"
            >
              <img
                :src="item.response.data.fileUrl"
                @click="previewJSBZ(item)"
                alt
              />
            </el-tag>
          </div>
        </template>

        <!-- 现场图片表单自定义 -->
        <template slot="xczpForm">
          <div class="xczp_content">
            <el-tag
              v-for="(item, index) in xczpFileList"
              :key="index"
              type="info"
              :closable="false"
              @close="handleRemoveXCZP(index)"
            >
              <img
                :src="item.response.data.fileUrl"
                @click="previewXCZP(item)"
                alt
              />
            </el-tag>
          </div>
        </template>
      </avue-crud>

      <!-- 弹窗：图片 -->
      <viewpicture
        :localPicBox="localPicBox"
        :localPictures="localPictures"
        :viewPicTitle="viewPicTitle"
        @confirmViewPicture="confirmViewPicture"
        @handleClosePicBox="handleClosePicBox"
      >
      </viewpicture>
    </basic-container>
  </div>
</template>

<script>
import {
  getList,
  getDeptList,
  getMainFaultList
} from "@/api/saferisk/riskpointcard";
import { mapGetters } from "vuex";
import { getToken } from "@/util/auth";
import { column } from "./constant/riskpointcard.js";
import viewpicture from "./components/viewpicture.vue";

export default {
  components: {
    viewpicture
  },
  data() {
    return {
      data: [],
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: this.$pageSize,
        currentPage: 1,
        total: 0
      },
      option: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchSpan: 5,
        searchMenuSpan: 4,
        refreshBtn: true,
        columnBtn: false,
        border: true,
        viewBtn: true,
        selection: false,
        dialogClickModal: false,
        menuWidth: 200,
        menuTitle: "风险点信息",
        editBtnText: "修改",
        addBtn: false,
        editBtn: false,
        delBtn: false,
        viewTitle: "查看风险点辨识",
        column: column
      },
      // 警示标志
      jsbzFileList: [],
      // 现场照片
      xczpFileList: [],
      // 地图
      mapPosition: {
        lng: 121.447254,
        lat: 31.32362
      },
      center: {
        lng: 121.447254,
        lat: 31.32362
      },
      zoom: 10,
      dragging: false,
      // 主要事故列表
      mainFaults: {},
      // 弹窗：查看图片
      localPicBox: false,
      localPictures: [],
      viewPicTitle: "",
      fxdjObj: {
        "1": "A",
        "2": "B",
        "3": "C",
        "4": "D"
      }
    };
  },
  computed: {
    ...mapGetters(["permission"])
  },
  watch: {
    // 表单经度
    "form.jd"(val) {
      if (val) {
        this.mapPosition.lng = val / 1;
      }
    },

    // 表单纬度
    "form.wd"(val) {
      if (val) {
        this.mapPosition.lat = val / 1;
      }
    }
  },
  mounted() {
    this.initData();
  },
  methods: {
    // 初始化数据
    initData() {
      var tenantId = JSON.parse(localStorage.getItem("saber-tenantId")).content;
      getDeptList(tenantId)
        .then(res => {
          const column = this.findObject(this.option.column, "ssbm");
          column.dicData = res.data.data;
        })
        .then(() => {
          this.getMainFaults();
        });
    },

    // 获取主要事故列表
    getMainFaults() {
      getMainFaultList()
        .then(res => {
          this.mainFaults = {};
          res.data.data.forEach(item => {
            this.mainFaults[item.dictKey] = item.dictValue;
          });
        })
        .then(() => {
          this.onLoad(this.page);
        });
    },

    // 显示主要事故列
    formatMainFault(row) {
      if (row.zysglx === undefined || typeof row.zysglx === "object") {
        return "";
      }
      var arr = row.zysglx.split(",");
      var faults = [];
      for (let i = 0; i < arr.length; i++) {
        var fault =
          this.mainFaults[arr[i]] === undefined
            ? arr[i]
            : this.mainFaults[arr[i]];
        faults.push(fault);
      }
      return faults.join(",");
    },

    // 导出
    handleExport() {
      this.$confirm("是否导出风险点辨识列表?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let fxdmc =
          this.query.fxdmc !== undefined &&
          this.query.fxdmc !== "" &&
          this.query.fxdmc !== null
            ? this.query.fxdmc
            : "";
        let fxfl =
          this.query.fxfl !== undefined &&
          this.query.fxfl !== "" &&
          this.query.fxfl !== null
            ? this.query.fxfl
            : "";
        let ssbm =
          this.query.ssbm !== undefined &&
          this.query.ssbm !== "" &&
          this.query.ssbm !== null
            ? this.query.ssbm
            : "";
        let fxdj =
          this.query.fxdj !== undefined &&
          this.query.fxdj !== "" &&
          this.query.fxdj !== null
            ? this.query.fxdj
            : "";
        window.open(
          `/api/riskPoint/riskmanagement/export?${
            this.website.tokenHeader
          }=${getToken()}&fxdmc=${fxdmc}&fxfl=${fxfl}&ssbm=${ssbm}&fxdj=${fxdj}`
        );
      });
    },

    // 新增、查看、编辑弹窗打开前
    beforeOpen(done, type) {
      this.xczpFileList = [];
      this.form.bladeFiles.forEach(item => {
        this.xczpFileList.push({
          name: item.fileName,
          response: {
            code: 200,
            data: item,
            msg: "操作成功",
            success: true
          }
        });
      });
      this.jsbzFileList = [];
      this.form.identifyFiles.forEach(item => {
        this.jsbzFileList.push({
          name: item.fileName,
          response: {
            code: 200,
            data: item,
            msg: "操作成功",
            success: true
          }
        });
      });

      this.center = {
        lng: this.form.jd,
        lat: this.form.wd
      };
      this.zoom = 13;
      done();
    },

    // 清空搜索条件
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },

    // 搜索条件改变事件
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },

    // 换页数
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },

    // 换每页显示数量
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },

    // 刷新
    refreshChange() {
      this.onLoad(this.page, this.query);
    },

    // 获取表格数据
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },

    /**
     * 弹窗：图片
     */

    // 查看现场照片
    handleViewPhoto(row) {
      this.localPictures = row.bladeFiles;
      this.localPicBox = true;
      this.viewPicTitle = "现场图片";
    },

    // 点击图片列表中已上传的现场图片
    previewXCZP(file) {
      if (
        file.response &&
        file.response.code === 200 &&
        file.response.data &&
        file.response.data.fileUrl
      ) {
        this.localPictures = [file.response.data];
        this.localPicBox = true;
        this.viewPicTitle = "现场图片";
      }
    },

    // 点击图片列表中已上传的警示图片
    previewJSBZ(file) {
      if (
        file.response &&
        file.response.code === 200 &&
        file.response.data &&
        file.response.data.fileUrl
      ) {
        this.localPictures = [file.response.data];
        this.localPicBox = true;
        this.viewPicTitle = "警示标识";
      }
    },

    // 确认照片
    confirmViewPicture() {
      this.localPictures = [];
      this.localPicBox = false;
      this.viewPicTitle = "";
    },

    // 关闭弹窗
    handleClosePicBox() {
      this.confirmViewPicture();
    }
  }
};
</script>

<style lang="scss">
@import "./styles/riskpointglobal.scss";
</style>

<style lang="scss" scpoed>
@import "./styles/riskpointscoped.scss";
</style>